package tree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class isSymmetric {
//    public boolean isSymmetric(TreeNode root) {
//        if (root == null){
//            return true;
//        }
//        return commpare(root.left, root.right);
//    }
//    public boolean commpare(TreeNode left, TreeNode right){
//        if (left == null && right != null) return false;
//        if (left != null && right == null) return false;
//        if (left == null && right == null) return true;
//        if (left.val != right.val) return false;
//        boolean outside = commpare(left.left, right.right);
//        boolean inside = commpare(left.right, right.left);
//        return outside && inside;
//    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null){
            return true;
        }
        Deque<TreeNode> que = new LinkedList<>();
        que.offer(root.left);
        que.offer(root.right);
        while (!que.isEmpty()){
            TreeNode leftNode = que.pollFirst();
            TreeNode rightNode = que.pollFirst();
            if (leftNode == null && rightNode == null){
                continue;
            }
            if (leftNode == null || rightNode == null || leftNode.val != rightNode.val){
                return false;
            }
            que.offerLast(leftNode.left);
            que.offerLast(rightNode.right);
            que.offerLast(leftNode.right);
            que.offerLast(rightNode.left);
        }
        return true;
    }
}
